package org.ktpro.shortlink.project.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.micrometer.core.annotation.Timed;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ktpro.shortlink.project.common.convention.result.Result;
import org.ktpro.shortlink.project.common.convention.result.Results;
import org.ktpro.shortlink.project.dto.req.ShortLinkBatchCreateReqDTO;
import org.ktpro.shortlink.project.dto.req.ShortLinkCreateReqDTO;
import org.ktpro.shortlink.project.dto.req.ShortLinkPageReqDTO;
import org.ktpro.shortlink.project.dto.req.ShortLinkUpdateReqDTO;
import org.ktpro.shortlink.project.dto.resp.ShortLinkBatchCreateRespDTO;
import org.ktpro.shortlink.project.dto.resp.ShortLinkCreateRespDTO;
import org.ktpro.shortlink.project.dto.resp.ShortLinkGroupCountQueryRespDTO;
import org.ktpro.shortlink.project.dto.resp.ShortLinkPageRespDTO;
import org.ktpro.shortlink.project.handle.CustomBlockHandler;
import org.ktpro.shortlink.project.service.ShortLinkService;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 短链接控制层
 */
@RestController
@Slf4j
@RequiredArgsConstructor
public class ShortLinkController {
    private final ShortLinkService shortlinkService;


    /**
     * 短链接重定向接口
     */
    @Timed(value = "project-restoreUrl", description = "短链接重定向")
    @GetMapping("/{short-uri}")
    public void restoreUrl(@PathVariable("short-uri") String shortUri,
                           ServletRequest request, ServletResponse response) throws IOException {
        shortlinkService.restoreUrl(shortUri, request, response);
    }


    /**
     * 创建短链接
     */
    @Timed(value = "project-create", description = "短链接创建")
    @PostMapping("/api/short-link/v1/create")
    @SentinelResource(
            value = "create_short-link",
            blockHandler = "createShortLinkBlockHandlerMethod",
            blockHandlerClass = CustomBlockHandler.class
    )
    public Result<ShortLinkCreateRespDTO> create(@RequestBody ShortLinkCreateReqDTO requestParam) {
        return Results.success(shortlinkService.create(requestParam));
    }

    /**
     * 批量创建短链接
     */
    @PostMapping("/api/short-link/v1/create/batch")
    public Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam) {
        return Results.success(shortlinkService.batchCreateShortLink(requestParam));
    }

    /**
     * 修改短链接信息
     */
    @Timed(value = "project-update", description = "短链接更新")
    @PostMapping("/api/short-link/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO requestParam) {
        shortlinkService.updateShortLink(requestParam);
        return Results.success();
    }


    /**
     * 短链接分页查询
     */
    @GetMapping("/api/short-link/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam) {
        return Results.success(shortlinkService.pageShortLink(requestParam));
    }

    /**
     * 查询短链接所有分组下的短链接个数
     */
    @GetMapping("/api/short-link/v1/count")
    public Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(
            @RequestParam("requestParam") List<String> requestParam) {
        return Results.success(shortlinkService.listGroupShortLinkCount(requestParam));
    }


}
